package com.changhai.diabetes.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.changhai.diabetes.model.LabBlood;
import com.changhai.diabetes.model.LabGanshen;
import com.changhai.diabetes.model.LabLipids;
import com.changhai.diabetes.model.LabOther;
import com.changhai.diabetes.model.LabPancreatCtai;
import com.changhai.diabetes.model.LabPancreatOgtt;
import com.changhai.diabetes.model.LabPancreatYidaosu;
import com.changhai.diabetes.model.LabPhysical;
import com.changhai.diabetes.model.LabSugar;
import com.changhai.diabetes.model.LabUrine;
import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.service.LabService;

/**
 * 实验室检查
 * 
 * @author Andy
 * 
 */
public class LabAction extends BaseAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private Long patientid;
	private LabPhysical labphy;
	private LabSugar labsugar;
	private LabGanshen labganshen;
	private LabLipids lablipids;
	private LabOther labother;
	private LabUrine laburine;
	private LabBlood labblood;
	private LabPancreatOgtt pancreatOgtt;
	private LabPancreatYidaosu pancreatYidaosu;
	private LabPancreatCtai pancreatCtai;
	private LabService labService;
	private Integer style;
	private Integer flag = 0;
	
	private Integer testmethod = 1;
	private Integer testtime = 1;

	private Integer ogttSel = 0;
	private Integer yidaoSel = 0;
	private Integer ctaiSel = 0;

	public String execute() throws Exception {
		return super.execute();
	}

	public String query() {
		return "input";
	}

	public String insertNewLabPhy() {
		labphy.setPatientid(patientid);
		tbList = this.getTableForEnter(patientid,flag);
		return "input";
	}

	public String editLabPhy() {
		if (patientid != null && patientid > 0) {
			try {
				labphy = labService.searchPhyByPatientId(patientid);
				tbList = this.getTableForEnter(patientid,flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无体格信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditLabPhy() {
		if (patientid == null || patientid <= 0 || labphy == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labphy.setStyle(style);
				labService.saveEditLabPhy(labphy);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "体格检查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabPhy();
	}

	public String saveNewLabPhy() {
		if (patientid == null || patientid <= 0 || labphy == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveNewLabPhy(labphy);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "体格检查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabPhy();
	}

	public String searchLabSugar() {
		if (patientid != null && patientid > 0) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("patientid", patientid);
				map.put("testtime", testtime);
				map.put("testmethod", testmethod);
				labsugar = labService.searchSugarByTimeAndType(map);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String editLabSugar() {
		if (patientid != null && patientid > 0) {
			try {
				if (labsugar == null) {
					labsugar = labService.searchSugarByPatientId(patientid);
				}
				tbList = this.getTableForEnter(patientid,flag);
				testtime = labsugar.getTesttime();
				testmethod = labsugar.getTestmethod();
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditLabSugar() {
		if (patientid == null || patientid <= 0 || labsugar == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labsugar.setTesttime(testtime);
				labsugar.setTestmethod(testmethod);
				labService.saveEditLabSugar(labsugar);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "血糖", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabSugar();
	}

	public String saveNewLabSugar() {
		if (patientid == null || patientid <= 0 || labsugar == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labsugar.setTesttime(testtime);
				labsugar.setTestmethod(testmethod);
				labService.saveNewLabSugar(labsugar);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "血糖", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabSugar();
	}

	public String editLabBlood() {
		if (patientid != null && patientid > 0) {
			try {
				labblood = labService.searchBloodByPatientId(patientid);
				tbList = this.getTableForEnter(patientid,flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditLabBlood() {
		if (patientid == null || patientid <= 0 || labblood == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveEditLabBlood(labblood);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "血常规", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabBlood();
	}

	public String saveNewLabBlood() {
		if (patientid == null || patientid <= 0 || labblood == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveNewLabBlood(labblood);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "血常规", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabBlood();
	}

	public String editLabGanshen() {
		if (patientid != null && patientid > 0) {
			try {
				labganshen = labService.searchGanshenByPatientId(patientid);
				tbList = this.getTableForEnter(patientid,flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditLabGanshen() {
		if (patientid == null || patientid <= 0 || labganshen == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveEditLabGanshen(labganshen);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "肝肾", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabGanshen();
	}

	public String saveNewLabGanshen() {
		if (patientid == null || patientid <= 0 || labganshen == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveNewLabGanshen(labganshen);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "肝肾", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabGanshen();
	}

	public String editLabLipids() {
		if (patientid != null && patientid > 0) {
			try {
				lablipids = labService.searchLipidsByPatientId(patientid);
				tbList = this.getTableForEnter(patientid,flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditLabLipids() {
		if (patientid == null || patientid <= 0 || lablipids == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveEditLabLipids(lablipids);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "血脂", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabLipids();
	}

	public String saveNewLabLipids() {
		if (patientid == null || patientid <= 0 || lablipids == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveNewLabLipids(lablipids);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "血脂", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabLipids();
	}

	public String editLabOther() {
		if (patientid != null && patientid > 0) {
			try {
				labother = labService.searchOtherByPatientId(patientid);
				tbList = this.getTableForEnter(patientid,flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditLabOther() {
		if (patientid == null || patientid <= 0 || labother == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveEditLabOther(labother);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "其他指标", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabOther();
	}

	public String saveNewLabOther() {
		if (patientid == null || patientid <= 0 || labother == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveNewLabOther(labother);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "其他指标", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabOther();
	}

	public String editLabUrine() {
		if (patientid != null && patientid > 0) {
			try {
				laburine = labService.searchUrineByPatientId(patientid);
				tbList = this.getTableForEnter(patientid,flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditLabUrine() {
		if (patientid == null || patientid <= 0 || laburine == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveEditLabUrine(laburine);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "尿液", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabUrine();
	}

	public String saveNewLabUrine() {
		if (patientid == null || patientid <= 0 || laburine == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				labService.saveNewLabUrine(laburine);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "尿液", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabUrine();
	}
/*
	public String insertNewLabPancreat() {
		pancreatOgtt.setPatientid(patientid);
		pancreatYidaosu.setPatientid(patientid);
		pancreatCtai.setPatientid(patientid);
		tbList = this.getTableForEnter(patientid,flag);
		return "input";
	}
*/
	public String editLabPancreat() {
		if (patientid != null && patientid > 0) {
			try {	
				pancreatOgtt = labService.searchOgttByPatientId(patientid);
				pancreatYidaosu = labService.searchYidaosuByPatientId(patientid);
				pancreatCtai = labService.searchCtaiByPatientId(patientid);	 
				tbList = this.getTableForEnter(patientid,flag);
				ogttSel = pancreatOgtt.getXtlevel();
				yidaoSel = pancreatYidaosu.getYdlevel();
				ctaiSel = pancreatCtai.getCtlevel();
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无相关信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String searchLabOgtt() throws IOException {
		PrintWriter wr = ServletActionContext.getResponse().getWriter();
		String res = "0";
		if (patientid != null && patientid > 0) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("patientid", patientid);
				map.put("xtlevel", ogttSel);
				pancreatOgtt = labService.searchOgttByPatientIdAndType(map);
				if (pancreatOgtt != null) {
					JSONArray json = JSONArray.fromObject(pancreatOgtt);
					res = json != null ? json.toString() : "0";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		wr.write(res);
		return NONE;
	}

	public String searchLabYidaosu() throws IOException {
		PrintWriter wr = ServletActionContext.getResponse().getWriter();
		String res = "0";
		if (patientid != null && patientid > 0) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("patientid", patientid);
				map.put("ydlevel", yidaoSel);
				pancreatYidaosu = labService.searchYidaosuByPatientIdAndType(map);
				if (pancreatYidaosu != null) {
					JSONArray json = JSONArray.fromObject(pancreatYidaosu);
					res = json != null ? json.toString() : "0";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		wr.write(res);
		return NONE;
	}

	public String searchLabCtai() throws IOException {
		PrintWriter wr = ServletActionContext.getResponse().getWriter();
		String res = "0";
		if (patientid != null && patientid > 0) {
			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("patientid", patientid);
				map.put("ctlevel", ctaiSel);
				pancreatCtai = labService.searchCtaiByPatientIdAndType(map);
				if (pancreatCtai != null) {
					JSONArray json = JSONArray.fromObject(pancreatCtai);
					res = json != null ? json.toString() : "0";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		wr.write(res);
		return NONE;
	}

	public String saveEditLabPancreat() {
		if (patientid == null || patientid <= 0 || pancreatOgtt == null || pancreatYidaosu == null
				|| pancreatCtai == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				if (ogttSel > 0) {
					pancreatOgtt.setXtlevel(ogttSel);
					pancreatOgtt.setPatientid(patientid);
					labService.saveEditLabOgtt(pancreatOgtt);
				}

				if (yidaoSel > 0) {
					pancreatYidaosu.setYdlevel(yidaoSel);
					pancreatYidaosu.setPatientid(patientid);
					labService.saveEditLabYidaosu(pancreatYidaosu);
				}
				if (ctaiSel > 0) {
					pancreatCtai.setCtlevel(ctaiSel);
					pancreatCtai.setPatientid(patientid);
					labService.saveEditLabCtai(pancreatCtai);
				}
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "胰岛功能", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabPancreat();
	}

	public String saveNewLabPancreat() {
		if (patientid == null || patientid <= 0 || pancreatOgtt == null || pancreatYidaosu == null
				|| pancreatCtai == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				pancreatOgtt.setXtlevel(ogttSel);
				pancreatYidaosu.setYdlevel(yidaoSel);
				pancreatCtai.setCtlevel(ctaiSel);
				labService.saveNewLabOgtt(pancreatOgtt);
				labService.saveNewLabYidaosu(pancreatYidaosu);
				labService.saveNewLabCtai(pancreatCtai);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "胰岛功能", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editLabPancreat();
	}

	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public Long getPatientid() {
		return patientid;
	}

	public void setPatientid(Long patientid) {
		this.patientid = patientid;
	}

	public LabPhysical getLabphy() {
		return labphy;
	}

	public void setLabphy(LabPhysical labphy) {
		this.labphy = labphy;
	}

	public LabSugar getLabsugar() {
		return labsugar;
	}

	public LabGanshen getLabganshen() {
		return labganshen;
	}

	public void setLabganshen(LabGanshen labganshen) {
		this.labganshen = labganshen;
	}

	public LabLipids getLablipids() {
		return lablipids;
	}

	public void setLablipids(LabLipids lablipids) {
		this.lablipids = lablipids;
	}

	public LabOther getLabother() {
		return labother;
	}

	public void setLabother(LabOther labother) {
		this.labother = labother;
	}

	public LabUrine getLaburine() {
		return laburine;
	}

	public void setLaburine(LabUrine laburine) {
		this.laburine = laburine;
	}

	public void setLabsugar(LabSugar labsugar) {
		this.labsugar = labsugar;
	}

	public LabService getLabService() {
		return labService;
	}

	public void setLabService(LabService labService) {
		this.labService = labService;
	}

	public LabBlood getLabblood() {
		return labblood;
	}

	public void setLabblood(LabBlood labblood) {
		this.labblood = labblood;
	}

	public Integer getOgttSel() {
		return ogttSel;
	}

	public void setOgttSel(Integer ogttSel) {
		this.ogttSel = ogttSel;
	}

	public Integer getTestmethod() {
		return testmethod;
	}

	public void setTestmethod(Integer testmethod) {
		this.testmethod = testmethod;
	}

	public Integer getTesttime() {
		return testtime;
	}

	public void setTesttime(Integer testtime) {
		this.testtime = testtime;
	}

	public LabPancreatOgtt getPancreatOgtt() {
		return pancreatOgtt;
	}

	public void setPancreatOgtt(LabPancreatOgtt pancreatOgtt) {
		this.pancreatOgtt = pancreatOgtt;
	}

	public LabPancreatYidaosu getPancreatYidaosu() {
		return pancreatYidaosu;
	}

	public void setPancreatYidaosu(LabPancreatYidaosu pancreatYidaosu) {
		this.pancreatYidaosu = pancreatYidaosu;
	}

	public LabPancreatCtai getPancreatCtai() {
		return pancreatCtai;
	}

	public void setPancreatCtai(LabPancreatCtai pancreatCtai) {
		this.pancreatCtai = pancreatCtai;
	}

	public Integer getYidaoSel() {
		return yidaoSel;
	}

	public void setYidaoSel(Integer yidaoSel) {
		this.yidaoSel = yidaoSel;
	}

	public Integer getCtaiSel() {
		return ctaiSel;
	}

	public void setCtaiSel(Integer ctaiSel) {
		this.ctaiSel = ctaiSel;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public Integer getStyle() {
		return style;
	}

	public void setStyle(Integer style) {
		this.style = style;
	}
}
